﻿//-----------------------------------------------------------------------
// <copyright file="WpfHelpers.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.Utility
{
    using System;
    using System.Windows;
    using System.Windows.Interop;

    /// <summary>
    /// Helpers for WPF
    /// </summary>
    public static class WpfHelpers
    {
        /// <summary>
        /// Get the screen that a window is on
        /// </summary>
        /// <param name="window">the window to start with</param>
        /// <returns>the screen</returns>
        public static System.Windows.Forms.Screen GetScreen(this Window window)
        {
            return System.Windows.Forms.Screen.FromHandle(new WindowInteropHelper(window).Handle);
        }

        /// <summary>
        /// Center the child window in th other window
        /// </summary>
        /// <param name="childWindow">the window to move</param>
        /// <param name="parentWindow">the window to align the other window to</param>
        public static void CenterInWindow(this Window childWindow, Window parentWindow)
        {
            if (parentWindow != null)
            {
                if (double.IsNaN(childWindow.ActualWidth) && double.IsNaN(childWindow.ActualHeight))
                {
                    // size not known yet. Try near top left
                    childWindow.Left = parentWindow.Left + (parentWindow.ActualWidth / 3);
                    childWindow.Top = parentWindow.Top + (parentWindow.ActualHeight / 3);                                        
                }
                else
                {
                    childWindow.Left = parentWindow.Left + ((parentWindow.ActualWidth - childWindow.ActualWidth) / 2);
                    childWindow.Top = parentWindow.Top + ((parentWindow.ActualHeight - childWindow.ActualHeight) / 2);                    
                }
            }
        }
    }
}
